<?php
    header("content-type:text/html;charset=utf-8");
    session_start();
    $config = include_once('../public/config/config.php');
    include_once('../public/func/db.php');
    include_once('../public/func/shangchuan.func.php');
    $table = 'article';
    $filename = '../public/upload';
    $_POST['ctime'] = time();
    $_POST['status'] = '未审核';
    $_POST['pic'] = shangchuan($_FILES['pic'],$filename);
    if($_POST['pic'] == '没有文件被上传'){
        $_POST['pic'] = 'default.jpg';
    }
    if(empty($_SESSION)){
        die("<script>alert('请先登录！');window.location.href='/home/login/login.php'</script>");
    }else{
        $arr = select($config,'user');
        foreach ($arr as $k => $v) {
            if($v['uname'] = $_SESSION['uname']){
                $_POST['uid'] = $v['id'];
            }
        }
        if(empty($_POST['title'])){
            die("<script>alert('标题不能为空');window.location.href='/home/addarticle.php'</script>");
        }else if(empty($_POST['content'])){
            die("<script>alert('内容不能为空');window.location.href='/home/addarticle.php'</script>");
        }
        $res = insert($config,$table,$_POST);
        if(!empty($res)){
            echo "<script>alert('发表博文失败！');window.location.href='/home/addarticle.php'</script>";
        }else{
            echo "<script>alert('发表博文成功！请等待审核！');window.location.href='/home/index.php'</script>";
        }
    }
?>